﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebApplication3.Helpers
{
	public static class DateTimeHelper
	{

		/// <summary>
		/// 获取时间戳
		/// </summary>
		/// <returns></returns>
		public static string GetTimeStamp(this System.DateTime time)
		{
			long ts = ConvertDateTimeToInt(time);
			return ts.ToString();
		}
		/// <summary>  
		/// 将c# DateTime时间格式转换为Unix时间戳格式  
		/// </summary>  
		/// <param name="time">时间</param>  
		/// <returns>long</returns>  
		public static long ConvertDateTimeToInt(this System.DateTime time)
		{
			System.DateTime startTime = System.TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
			return (long)(time - startTime).TotalSeconds;
		}
		 

		/// <summary>        
		/// 时间戳转为C#格式时间        
		/// </summary>        
		/// <param name=”timeStamp”></param>        
		/// <returns></returns>        
		private static DateTime ConvertStringToDateTime(string timeStamp)
		{
			DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
			long lTime = long.Parse(timeStamp + "0000");
			TimeSpan toNow = new TimeSpan(lTime);
			return dtStart.Add(toNow);
		}
	}
}
